﻿using Newtonsoft.Json;
using NoodlesFriedFood.PlcMake;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Threading;
using System.Windows;
using System.Windows.Documents;

namespace NoodlesFriedFood
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static List<StatusList> editDevicesMT = new List<StatusList>();
        public static List<StatusList> editDevicesZH = new List<StatusList>();
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                Thread thread = new Thread(PlcMakes.Instance.SelRead);
                thread.IsBackground = true;
                thread.Start();
                PlcMakes.Instance.Init();
                Thread thread1 = new Thread(EWMMsg.Instance.IOSerial_Thr);
                thread1.IsBackground = true;
                thread1.Start();
                //Thread thread2 = new Thread(MessageShow);
                //thread2.IsBackground = true;
                //thread2.Start();

            }
            catch (System.Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 全部订单查询
        /// </summary>
        public void MessageShow()
        {
            while (true)
            {
                try
                {
                    Device device = new Device();
                    string lists = JsonConvert.SerializeObject(device);
                    var data = HttpMsg.Instance.deviceOrderQueueListMsg(lists);
                    Console.WriteLine(data);
                    if (data != null)
                    {
                        var msglist = JsonConvert.DeserializeObject<HTTPMsg>(data);
                        Console.WriteLine(msglist);
                        if (msglist != null)
                        {
                            if (msglist.data.ToString().Length > 10)
                            {
                                var list = JsonConvert.DeserializeObject<List<deviceOrderQueueList>>(msglist.data.ToString());
                                Console.WriteLine("yes11");
                                if (list.Count > 0)
                                {
                                    if (list.Count > 0)
                                    {
                                        bool BOL = true;
                                        Console.WriteLine(PlcReadBol.Instance.Matter14);
                                        if (PlcReadBol.Instance.Matter14)
                                        {
                                            Console.WriteLine("yes12");
                                            foreach (var item in list)
                                            {
                                                Console.WriteLine("yes13");
                                                if (editDevicesMT.Count < 3)
                                                {
                                                    Console.WriteLine("yes14");
                                                    if (item.typeList2.Count > 0)
                                                    {
                                                        Console.WriteLine("yes15");
                                                        if (!PlcReadBol.Instance.Matter14)
                                                        {
                                                            break;
                                                        }
                                                        var dd = item.typeList2.FirstOrDefault(a => a.status == "3");
                                                        Console.WriteLine("yes16");
                                                        if (dd != null)
                                                        {
                                                            Console.WriteLine("yes18");
                                                            MakingShow(dd, item.orderNum);
                                                            Console.WriteLine("yes19");
                                                            Console.WriteLine(PlcReadBol.Instance.Matter14);
                                                            while (PlcReadBol.Instance.Matter14)
                                                            {
                                                                Thread.Sleep(100);
                                                            }
                                                            break;
                                                        }
                                                        Console.WriteLine("yes17");
                                                    }
                                                }
                                                else
                                                {
                                                    break;
                                                }
                                            }
                                        }
                                        if (PlcReadBol.Instance.Matter15)
                                        {
                                            foreach (var item in list)
                                            {
                                                if (editDevicesZH.Count < 2)
                                                {
                                                    if (item.typeList4.Count > 0)
                                                    {
                                                        Console.WriteLine("yes21");
                                                        if (!PlcReadBol.Instance.Matter15)
                                                        {
                                                            break;
                                                        }
                                                        Console.WriteLine("yes22");
                                                        var dd = item.typeList4.FirstOrDefault(a => a.status == "3");
                                                        if (dd != null)
                                                        {
                                                            Console.WriteLine("yes23");
                                                            var msgs = JsonConvert.DeserializeObject<List<specification>>(dd.jsonCodeVal);
                                                            Console.WriteLine(DateTime.Now.ToString() + "  cx10" + dd.jsonCodeVal);
                                                            if (msgs.Count > 0)
                                                            {
                                                                Console.WriteLine("yes24");
                                                                var coffeeID = msgs.FirstOrDefault(a => a.classCode != "");
                                                                if (coffeeID != null)
                                                                {
                                                                    Console.WriteLine("yes25");
                                                                    MakingZHShow(dd, item.orderNum, coffeeID.classCode == "3001" ? 1 : coffeeID.classCode == "3002" ? 2 : 3);
                                                                    Console.WriteLine("yes26");
                                                                    while (PlcReadBol.Instance.Matter15)
                                                                    {
                                                                        Thread.Sleep(100);
                                                                    }
                                                                }
                                                            }
                                                            Console.WriteLine("yes27");
                                                            break;
                                                         
                                                        }

                                                    }
                                                }
                                                else
                                                {
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    Thread.Sleep(2000);
                }
                catch (Exception)
                {
                    Thread.Sleep(2000);
                }
            }
        }
        public void MakingShow(DeviceOrderQueue list, string orderNum)
        {
            try
            {
                EditDeviceOrderStatus status = new EditDeviceOrderStatus();
                status.orderGoodsId = list.id;
                status.orderId = list.orderId;
                status.status = "4";
                if (list.status == "3")
                {
                    string lists = JsonConvert.SerializeObject(status);
                    StatusList statulist = new StatusList();
                    statulist.status = status;
                    statulist.id = editDevicesMT.Count == 0 ? 1 : PlcReadBol.Instance.Making1_1 && !(editDevicesMT.Any(a => a.id == 1)) ? 1 : PlcReadBol.Instance.Making1_2 && !(editDevicesMT.Any(a => a.id == 2)) ? 2 : 3;
                    editDevicesMT.Add(statulist);
                    while (true)
                    {
                        var ddd = HttpMsg.Instance.editDeviceOrderStatusMsg(lists);
                        if (ddd != null)
                        {
                            if (ddd != "")
                            {
                                Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd);
                                break;
                            }
                        }
                        Thread.Sleep(100);
                    }
                    Console.WriteLine("zuopin");
                    PlcMakes.Instance.readBol = false;
                    Thread.Sleep(500);
                    PlcMakes.Instance.UpdateInit("\"Com_Data\".Command.Action1");
                    Thread.Sleep(500);
                    PlcMakes.Instance.readBol = true;

                    Console.WriteLine("发送完成00");
                }
                else
                {
                    if (editDevicesMT.Count == 0)
                    {
                        StatusList statulist1 = new StatusList();
                        statulist1.status = status;
                        statulist1.id = !PlcReadBol.Instance.Making1_1 && !(editDevicesMT.Any(a => a.id == 1)) ? 1 : PlcReadBol.Instance.Making1_2 && !(editDevicesMT.Any(a => a.id == 2)) ? 2 : 3;
                        editDevicesMT.Add(statulist1);
                    }
                    else
                    {
                        if (!editDevicesMT.Any(a => a.status.orderId == status.orderId && a.status.orderGoodsId == status.orderGoodsId))
                        {
                            StatusList statulist1 = new StatusList();
                            statulist1.status = status;
                            statulist1.id = !PlcReadBol.Instance.Making1_1 && !(editDevicesMT.Any(a => a.id == 1)) ? 1 : PlcReadBol.Instance.Making1_2 && !(editDevicesMT.Any(a => a.id == 2)) ? 2 : 3;
                            editDevicesMT.Add(statulist1);
                        }
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        public void MakingZHShow(DeviceOrderQueue list, string orderNum, int orders)
        {
            try
            {
                EditDeviceOrderStatus status = new EditDeviceOrderStatus();
                status.orderGoodsId = list.id;
                status.orderId = list.orderId;
                status.status = "4";
                if (list.status == "3")
                {
                    string lists = JsonConvert.SerializeObject(status);
                    StatusList statulist = new StatusList();
                    statulist.status = status;
                    statulist.id = editDevicesZH.Count == 0 ? 1 : PlcReadBol.Instance.Making2_1 && !(editDevicesZH.Any(a => a.id == 1)) ? 1 : PlcReadBol.Instance.Making2_2 && !(editDevicesZH.Any(a => a.id == 2)) ? 2 : 1;
                    editDevicesZH.Add(statulist);
                    while (true)
                    {
                        var ddd = HttpMsg.Instance.editDeviceOrderStatusMsg(lists);
                        if (ddd != null)
                        {
                            if (ddd != "")
                            {
                                Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd);
                                break;
                            }
                        }
                        Thread.Sleep(100);
                    }
                    PlcMakes.Instance.readBol = false;
                    Thread.Sleep(500);
                    var method = orders == 1 ? "\"Com_Data\".Command.Order1" : orders == 2 ? "\"Com_Data\".Command.Order2" : "\"Com_Data\".Command.Order3";
                    PlcMakes.Instance.UpdateZHInit(method);
                    Thread.Sleep(500);
                    PlcMakes.Instance.readBol = true;
                }
                else
                {
                    if (editDevicesZH.Count == 0)
                    {
                        StatusList statulist1 = new StatusList();
                        statulist1.status = status;
                        statulist1.id = !PlcReadBol.Instance.Making2_1 && !(editDevicesZH.Any(a => a.id == 1)) ? 1 : PlcReadBol.Instance.Making2_2 && !(editDevicesMT.Any(a => a.id == 2)) ? 2 : 1;
                        editDevicesZH.Add(statulist1);
                    }
                    else
                    {
                        if (!editDevicesZH.Any(a => a.status.orderId == status.orderId && a.status.orderGoodsId == status.orderGoodsId))
                        {
                            StatusList statulist1 = new StatusList();
                            statulist1.status = status;
                            statulist1.id = !PlcReadBol.Instance.Making2_1 && !(editDevicesZH.Any(a => a.id == 1)) ? 1 : PlcReadBol.Instance.Making2_2 && !(editDevicesZH.Any(a => a.id == 2)) ? 2 : 1;
                            editDevicesZH.Add(statulist1);
                        }
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MakingShow(null, null);
            }
            catch (Exception)
            {
                return;
            }
        }
    }
}
